<template>
  <div class="app-container">

    <el-row :gutter="50" class="mb8">
      <el-col :sm="4" class="hidden-xs-only" style="opacity:0;">左侧占位</el-col>
      <el-col :span="16">
        <el-card class="box-card" shadow="always">
          <div slot="header" class="clearfix" style="text-align: center; font-size: 25px;">
            站长信息设置
            <el-button style="float: right; padding: 4px 0" type="text" size="small" @click="submitForm">修改信息</el-button>
          </div>
          <el-form ref="form" :model="form" :disabled="false" :rules="rules" label-width="80px">
              <div style="text-align: center;">
                <imageUpload :limit="1" v-model="form.avatarUrl"/>
              </div>
              <el-divider></el-divider>
              <!-- <el-form-item label="" style="text-align: center;">
                <imageUpload :limit="1" v-model="form.avatarUrl"/>
              </el-form-item> -->
              <el-form-item label="站长昵称" prop="nickeName">
                <el-input v-model="form.nickeName" placeholder="请输入站长昵称" />
              </el-form-item>
              <el-form-item label="联系QQ" prop="qqContact">
                <el-input v-model="form.qqContact" placeholder="请输入联系QQ" />
              </el-form-item>
              <el-form-item label="联系微信" prop="wxContact">
                <el-input v-model="form.wxContact" placeholder="请输入联系微信" />
              </el-form-item>
              <el-form-item label="联系邮箱" prop="emailContact">
                <el-input v-model="form.emailContact" placeholder="请输入联系邮箱" />
              </el-form-item>
              <el-form-item label="联系手机" prop="phoneContact">
                <el-input v-model="form.phoneContact" placeholder="请输入联系手机" />
              </el-form-item>
              <el-form-item label="GitHub" prop="githubUrl">
                <el-input v-model="form.githubUrl" placeholder="GitHub 地址" />
              </el-form-item>
              <el-form-item label="Gitee" prop="giteeUrl">
                <el-input v-model="form.giteeUrl" placeholder="Gitee 地址" />
              </el-form-item>
              <el-form-item label="个人简介" prop="synopsis">
                <el-input v-model="form.synopsis" type="textarea" placeholder="请输入内容" />
              </el-form-item>
          </el-form>
        </el-card>
      
      </el-col>
      <el-col :sm="4" class="hidden-xs-only" style="opacity:0;">右侧占位</el-col>
    </el-row>

      
  </div>
</template>

<script>

import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/cms/customer";

export default {
  name: "Customer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站长信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        avatarUrl: null,
        nickeName: null,
        synopsis: null,
        qqContact: null,
        wxContact: null,
        emailContact: null,
        phoneContact: null,
        githubUrl: null,
        giteeUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询站长信息列表 */
    getList() {
      // this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.form = response.rows[0];
        // this.customerList = response.rows;
        // this.total = response.total;
        // this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        avatarUrl: null,
        nickeName: null,
        synopsis: null,
        qqContact: null,
        wxContact: null,
        emailContact: null,
        phoneContact: null,
        githubUrl: null,
        giteeUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站长信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站长信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } 
          // else {
          //   addCustomer(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除站长信息编号为"' + ids + '"的数据项？').then(function() {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cms/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
